<template>
  <div class="app-container">
    <el-form
      :model="formInline"
      class="form-inline"
      ref="form"
      label-width="70px"
    >
      <el-row :gutter="4">
        <el-col :lg="6" :xl="4"
          ><el-form-item label="订单号" style="width: 100%">
            <el-input
              v-model="formInline.orderNum"
              placeholder="订单号"
            ></el-input> </el-form-item
        ></el-col>
        <el-col :lg="6" :xl="4"
          ><el-form-item label="保单状态" style="width: 100%">
            <el-select clearable v-model="formInline.reportStatus" style="width:100%">
                <el-option label="支付成功" value="2"></el-option>
                <el-option label="投保成功" value="3"></el-option>
                <el-option label="投保失败" value="4"></el-option>
              </el-select>
            </el-form-item
        ></el-col>
        <el-col :lg="6" :xl="4"
          ><el-form-item label="保单号" style="width: 100%">
            <el-input
              v-model="formInline.insureNum"
              placeholder="保单号"
            ></el-input> </el-form-item
        ></el-col>
        <el-col :lg="6" :xl="4">
          <!-- <el-form-item> -->
          <el-button type="primary" icon="el-icon-search" @click="getTableData">搜索</el-button>
          <!-- </el-form-item> -->
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="table"
      :data="tableData">
    <el-table-column align="center" prop="insureNum" label="保单号" width="220">
      </el-table-column>
      
      <el-table-column align="center" prop="boxNo" label="箱号/运单号/车号/厢号" width="180">
        <template slot-scope="scope">
          <div v-if="!!scope.row.containerNumber">箱号：{{ scope.row.containerNumber }}</div>
          <div v-if="!!scope.row.emsNumber">运单号：{{ scope.row.emsNumber }}</div>
          <div v-if="!!scope.row.carNumber">车号：{{ scope.row.carNumber }}</div>
          <div v-if="!!scope.row.vanNumber">厢号：{{ scope.row.vanNumber }}</div>
        </template>
      </el-table-column>
      
      <el-table-column align="center" prop="orderNum" label="订单号" width="220">
      </el-table-column>

      <el-table-column align="center" prop="insuredName" label="被保险人" width="180">
      </el-table-column>
      
      <el-table-column align="center" prop="reportStatus" label="保险出险状态">
        <template slot-scope="scope">
          {{
            scope.row.reportStatus === "0"
              ? "未出险"
              : scope.row.checkStatus === "1"
              ? "报案"
              : scope.row.checkStatus === "2"
              ? "立案"
              : scope.row.checkStatus === "3"
              ? "未立案"
              : scope.row.checkStatus === "4"
              ? "勘察/定损"
              : scope.row.checkStatus === "5"
              ? "核赔"
              : scope.row.checkStatus === "6"
              ? "付款"
              : scope.row.checkStatus === "6"
              ? "结案"
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column align="center" prop="orderNum" label="出险时间" width="220">
      </el-table-column>

      <el-table-column align="center" prop="reportStatus" label="订单状态">
        <template slot-scope="scope">
        <el-tag v-if="scope.row.orderStatus === '2'">支付成功</el-tag>
        <el-tag v-if="scope.row.orderStatus === '3'" type="success">投保成功</el-tag>
        <el-tag v-if="scope.row.orderStatus === '4'" type="danger">投保失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="insureFee" label="保险金额(元)"> </el-table-column>
      <el-table-column align="center" prop="goodsName" label="货物名称"> </el-table-column>
      <el-table-column align="center" width="270"  prop="createTime" label="投保时间"> </el-table-column>
      <el-table-column align="center" width="270" label="操作" fixed="right">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="view(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="returnPolicy(scope.row)"
            >回传保单</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            v-if="scope.row.orderStatus * 1 === 2"
             @click="cancel(scope.row)"
            >取消订单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pageNum"
      :page-sizes="[10, 20, 50]"
      :page-size="page.pageSize"
      layout="->, total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
    <view-or-examine @initTable="initTable" ref="viewOrExamine"></view-or-examine>
    <returnPolicyDialog @initTable="initTable" ref="returnPolicyDialog"></returnPolicyDialog>

    <el-dialog title="取消订单" :visible.sync="showCancelOrder">
      <el-form :model="cancelOrderForm">
        <el-form-item label="订单取消原因备注" :label-width="150">
          <el-input type="textarea" v-model="cancelOrderForm.orderStatusRemark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCancelOrder = false">取 消</el-button>
        <el-button type="primary" @click="confirmClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import viewOrExamine from "./viewOrExamine.vue";
import returnPolicyDialog from "./returnPolicyDialog.vue";

export default {
  components: { viewOrExamine, returnPolicyDialog },
  data() {
    return {
      showCancelOrder: false,
      orderRow: {},
      cancelOrderForm: {
        orderStatusRemark: ''
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      formInline: {
        orderNum: "", //保单号
        reportStatus: "", //保险出险状态
        insureNum: "", //保单号
      },
      tableData: [],
      tableHeight: 0
    };
  },
  methods: {
    cancel(orderRow) {
      this.showCancelOrder = true
      this.orderRow = orderRow
    },
    confirmClick() {
      const params = {
        warrantyId: this.orderRow.warrantyId,
        orderStatusRemark: this.cancelOrderForm.orderStatusRemark
      }
      this.$post(`/warranty/insure/cancelOrder`, params).then((res) => {
        this.$message.success('取消成功') // 成功提示信息
        this.showCancelOrder = true
        this.page.pageNum = 1
        this.getTableData()
      });
    },
    returnPolicy(row) {
      this.$refs.returnPolicyDialog.dialogFormVisible = true;
      this.$refs.returnPolicyDialog.row = row;
      this.$refs.returnPolicyDialog.insureUrl = row.insureUrl;
      this.$refs.returnPolicyDialog.form.insureNum = row.insureNum;
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    view(row) {
      this.$refs.viewOrExamine.dialogFormVisible = true;
      this.$refs.viewOrExamine.title = "查看";
      this.$refs.viewOrExamine.isExamine = false;
      this.$refs.viewOrExamine.row = row;
      this.$refs.viewOrExamine.insureUrl = row.insureUrl;
      this.$refs.viewOrExamine.form.insureNum = row.insureNum;
    },
    setTableHeight() {
      this.$nextTick(() => {
        const queryConditionHeight = this.$refs.form.$el.offsetHeight;
        this.tableHeight =
          window.innerHeight - queryConditionHeight - 180 + "px";
      });
    },
    initTable() {
      this.$refs.viewOrExamine.dialogFormVisible = false;
      this.page.pageNum = 1
      this.getTableData()
    },
    getTableData() {
      const params = {
        ...this.formInline,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      console.log(params, params)
      this.$get("/warranty/insure/list", params).then((res) => {
        this.tableData = res.rows;
        this.page.total = res.total;
      });
    },
    // 开始自动刷新
    startAutoRefresh() {
      this.refreshInterval = setInterval(this.getTableData, 60000); // 每60秒刷新一次数据
    },
    // 停止自动刷新
    stopAutoRefresh() {
      clearInterval(this.refreshInterval);
    },
  },
  mounted() {
    window.onresize = () => {
      this.setTableHeight();
    };
    this.setTableHeight();
    this.getTableData();
    this.startAutoRefresh(); // 开启自动刷新
  },
  beforeDestroy() {
    this.stopAutoRefresh(); // 组件销毁前停止刷新
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>
